<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: PasswordService.cfc 1048 2007-01-09 01:17:09Z pfarrell $

Notes:
--->
<cfcomponent
	displayname="PasswordService"
	output="false"
	hint="PasswordService for MachBlog">
	
	<!---
	PROPERTIES
	--->
	<cfset variables.udfs = "" />
	<cfset variables.datasource = "" />
	
	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="init" access="public" output="false" returntype="PasswordService" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setUdfs" access="public" output="false" returntype="void">
		<cfargument name="udfs" type="org.machblog.utils.udfs" required="true" />
		<cfset variables.udfs = arguments.udfs />
	</cffunction>
	<cffunction name="getUdfs" access="public" output="false" returntype="org.machblog.utils.udfs">
		<cfreturn variables.udfs />
	</cffunction>
	
	<cffunction name="setDatasource" access="public" output="false" returntype="void">
		<cfargument name="datasource" type="org.machblog.datasource.Datasource" required="true" />
		<cfset variables.datasource = arguments.datasource />
	</cffunction>
	<cffunction name="getDatasource" access="public" output="false" returntype="org.machblog.datasource.Datasource">
		<cfreturn variables.datasource />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS
	--->
	<cffunction name="getPasswordSalt" access="public" output="false" returntype="string" 
			hint="Returns the password salt for a particular user">
		<cfargument name="email" type="string" required="true" />
		
		<cfset var getSalt = "" />
		
		<cftry>
			<cfquery name="getSalt" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT password_salt 
				FROM machblog_user 
				WHERE email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="100" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfreturn getSalt.password_salt />
	</cffunction>
	
	<cffunction name="hashPassword" access="public" output="false" returntype="string" 
			hint="Hashes the password string passed in and returns the hashed version of the string">
		<cfargument name="password" type="string" required="true" />
		<cfreturn getUdfs().sha1(arguments.password) />
	</cffunction>
	
	<cffunction name="saltAndHashPassword" access="public" output="false" returntype="string" 
			hint="Convenience method to append the user's password salt to the password provided and then hash the password">
		<cfargument name="email" type="string" required="true" />
		<cfargument name="password" type="string" required="true" />
		<cfreturn hashPassword(arguments.password & getPasswordSalt(arguments.email)) />
	</cffunction>
	
	<cffunction name="generatePassword" access="public" output="false" returntype="string" 
			hint="Generates a random, strong password and returns it to the caller">
		<cfreturn getUdfs().randString("secure", 10) />
	</cffunction>
	
</cfcomponent>